//
//  ViewController.m
//  MailList
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 KPY. All rights reserved.
//

#import "ViewController.h"

#import "LyMailListManager.h"
#import "Student.h"
#import "LyNextViewController.h"
#import "LyTableViewCell.h"
#import "LyMoreViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) NSMutableArray *array;

@property(nonatomic, strong) LyMailListManager *manager;

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated {
    if (self.array.count == 0) {
        self.array = [NSMutableArray array];
    }
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    NSPredicate *pre = [NSPredicate predicateWithValue:YES];
    request.predicate = pre;
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    self.array = [NSMutableArray arrayWithArray:array];
    
    [self.tableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 创建数据管理器
    self.manager = [LyMailListManager defaults];
    
    [self.manager applicationDocumentsDirectory];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.tableView.rowHeight = 150;
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction:)];
    
    
    
    
}

- (void)addAction:(UIBarButtonItem *)button {
    
    LyNextViewController *next = [[LyNextViewController alloc] init];
    
    [self.navigationController pushViewController:next animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.array.count != 0) {
        
        static NSString *reuse = @"reuse";
        
        LyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        
        if (!cell) {
            
            cell = [[LyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            
        }
        
        Student *stu = self.array[indexPath.row];
        
        cell.nameLabel.text = [NSString stringWithFormat:@"姓名         %@", stu.name];
        
        cell.sexLabel.text = [NSString stringWithFormat:@"性别         %@", stu.sex];
        
        cell.numberLabel.text = [NSString stringWithFormat:@"学号        %@", stu.number];
        
        return cell;
    } else {
        
        static NSString *reuse = @"reuse";
        
        LyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        
        if (!cell) {
            
            cell = [[LyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            
        }
        return cell;

    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LyMoreViewController *next = [[LyMoreViewController alloc] init];
    
    [self.navigationController pushViewController:next animated:YES];
    
    Student *stu = self.array[indexPath.row];
    
    next.temp = stu;
    
}

//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    return UITableViewCellEditingStyleDelete;
//    
//}

//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//    // 这个方法默认是所有行都可以进行编辑
//    //    if (indexPath.row % 2 == 0) {
//    //        return NO;
//    //    } else {
//    //        return YES;
//    //    }
//    return YES;
//}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 删除一定要删除对应数据源的数据
        [self.array removeObjectAtIndex:indexPath.row];
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        if (self.array.count != 0) {
        Student *stu = [[Student alloc] init];
            stu = self.array[indexPath.row];
            
            NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
            
            NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], stu.name];
            
            request.predicate = predicate;
            
            NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
            
            [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
            
            NSError *error = nil;
            
            NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
            
            for (Student *stu in array) {
                
                [self.manager.managedObjectContext deleteObject:stu];
                
            }
            
            
            [self.manager saveContext];
        
        } else {
            
            NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
            
            NSError *error = nil;
            
            NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
            
            for (Student *stu in array) {
                
                [self.manager.managedObjectContext deleteObject:stu];
                
            }
            
            
            [self.manager saveContext];
            
        
            
        }
        [self.tableView reloadData];
        
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
